﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Newtonsoft.Json.Linq;
using WeiboPlus.Models;
using WeiboPlus.Service;

namespace WeiboPlus.Sina
{
    public class SinaJTokenToTrendMapping : MappingBase<JToken, SNSTrend>
    {
        public SinaJTokenToTrendMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSTrend Map(JToken s)
        {
            try
            {
                var trend = new SNSTrend();

                if (s.SelectToken("name") != null)
                {
                    trend.Name = (string)s.SelectToken("name");
                }

                if (s.SelectToken("query") != null)
                {
                    trend.Query = (string)s.SelectToken("query");
                }

                return trend;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return null;
                //throw;
            }
            //throw new NotImplementedException();
        }
    }

    public class SinaJTokenToTrendsMapping : MappingBase<JToken, SNSTrends>
    {
        public SinaJTokenToTrendsMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSTrends Map(JToken s)
        {
            try
            {
                var trends = new SNSTrends();

                if (s.SelectToken("trends") != null && s.SelectToken("trends").SelectToken(System.DateTime.Now.ToString("yyyy-MM-dd")) != null)
                {
                    var tokens = s.SelectToken("trends").SelectToken(System.DateTime.Now.ToString("yyyy-MM-dd"));

                    foreach (var token in tokens)
                    {
                        var trend = _factory.GetMapping<JToken, SNSTrend>().Map(token);
                        if (trend != null)
                        {
                            trends.Trends.Add(trend);
                        }
                    }
                }

                return trends;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                Debug.WriteLine(s);
                return null;
                //throw;
            }
            //throw new NotImplementedException();
        }
    }
}
